package com.web.portal.service;

import java.util.EnumMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.web.portal.dao.AdminDao;
import com.web.portal.dao.BaseDao;
import com.web.portal.dao.WebportalSearchType;
import com.web.portal.entity.Admin;

@Service(value = "adminService")
public class DefaultAdminService extends DefaultWebPortalService<Admin>
		implements AdminService {
	private static final Logger LOG = Logger
			.getLogger(DefaultAdminService.class);

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	protected boolean isCreate(Admin entity) throws RuntimeException {
		// TODO Auto-generated method stub
		return false;
	}
	@Autowired
	@Qualifier(value="adminDao")
	public void init(BaseDao dao){
		setBaseDao(dao);
	}
	

	public Admin getByUserNameAndPassword(String userName, String password) {
		// TODO Auto-generated method stub
		final EnumMap<WebportalSearchType, Object> searchCriteria = new EnumMap(
				WebportalSearchType.class);
		searchCriteria.put(WebportalSearchType.USER_NAME, userName);
		searchCriteria.put(WebportalSearchType.PASSWORD, password);
		List<Admin> result = searchByCriteria(searchCriteria);
		if (!result.isEmpty() && result.size() == 1) {
			LOG.debug("unique username and password found");
			return result.get(0);
		}
		LOG.debug("multiple username and password found for same user");
		return null;

	}

	public boolean isUserNamePasswordValid(String userName, String password) {
		final EnumMap<WebportalSearchType, Object> searchCriteria = new EnumMap(
				WebportalSearchType.class);
		searchCriteria.put(WebportalSearchType.USER_NAME, userName);
		searchCriteria.put(WebportalSearchType.PASSWORD, password);
		List<Admin> result = searchByCriteria(searchCriteria);
		if (!result.isEmpty() && result.size() == 1) {
			LOG.debug("unique username and password found");
			return true;
		}
		LOG.debug("multiple username and password found for same user");
		return false;

	}
	public Admin getByUserName(String userName) {
		// TODO Auto-generated method stub
		final EnumMap<WebportalSearchType, Object> searchCriteria = new EnumMap(
				WebportalSearchType.class);
		searchCriteria.put(WebportalSearchType.USER_NAME, userName);
		
		List<Admin> result = searchByCriteria(searchCriteria);
		if (!result.isEmpty() && result.size() == 1) {
			LOG.debug("unique username and password found");
			return result.get(0);
		}
		LOG.debug("multiple username and password found for same user");
		return null;

	}
	public List<Admin> getAdmin(Admin admin) {
		// TODO Auto-generated method stub
		return null;
	}
	}

